package com.webware.one.client.popup;

import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.UIObject;

public class Popup {
  public static void info(final String title, final String text, final IPopupReturnHandler<Void> handler) {
    AbstractPopupWindow dialogBox = new AbstractPopupWindow(title, new HTML(text), EButton.CLOSE) {
      @Override
      public void onFinish(EButton choice) {
        if (handler != null) handler.handle(choice, null);
      }
    };
    dialogBox.center();
    dialogBox.setFocuse(EButton.CLOSE);
  }

  /** Всплывающая подсказка относительно заданного элемента */
  public static void hint(final UIObject target, final String text) {
    if (text != null && !text.isEmpty()) {
      final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
      // simplePopup.setWidth("150px");
      simplePopup.setWidget(new HTML(text));

      simplePopup.showRelativeTo(target);
    }
  }

  public static void input(final String title, final String text, final IPopupReturnHandler<String> handler) {
    final TextBox tb = new TextBox();
    tb.setWidth("100%");
    Grid grid = new Grid(1, 2);
    grid.setWidth("100%");
    grid.setText(0, 0, text);
    grid.setWidget(0, 1, tb);

    AbstractPopupWindow dialogBox = new AbstractPopupWindow(title, grid, EButton.NO, EButton.YES) {
      @Override
      public void onFinish(EButton choice) {
        if (handler != null) handler.handle(choice, EButton.YES.equals(choice) ? tb.getText() : null);
      }
    };
    dialogBox.center();
    dialogBox.setFocuse(EButton.CLOSE);
  }
}
